home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 July / macformat52.iso / mac / Shareware Plus / Developers / YAAF v1.0 alpha 1 / (Sources) / Standard Controls / Text / XGStdEditText.cpp next >
Encoding:
C/C++ Source or Header  |  1997-04-24  |  10.6 KB  |  544 lines

  1. /*    XGStdEditText.cpp
  2.  *
  3.  *        This handles the standard editing control.
  4.  */
  5.  
  6. /*  YAAF - Yet another application framework
  7.  *  Copyright (C) 1997 William Edward Woody and In Phase Consulting
  8.  *  
  9.  *  This library is free software; you can redistribute it
  10.  *  and/or modify it under the terms of the GNU Library
  11.  *  General Public License as published by the Free Software
  12.  *  Foundation; either version 2 of the License, or any
  13.  *  later version.
  14.  *  
  15.  *  This library is distributed in the hope that it will be
  16.  *  useful, but WITHOUT ANY WARRANTY; without even the implied
  17.  *  warranty of MERCHANTABIILITY or FITNESS FOR A PARTICULAR
  18.  *  PURPOSE. See the GNU Library General Public License for
  19.  *  more details.
  20.  *  
  21.  *  You should have received a copy of the GNU Library General
  22.  *  Public License along with this library; if not, write to the
  23.  *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24.  *  Boston, MA 02111-1307, USA.
  25.  *  
  26.  *  To contact the author, either e-mail me at
  27.  *  woody@alumni.caltech.edu, or write to us at
  28.  *  
  29.  *          William Edward Woody
  30.  *          In Phase Consulting
  31.  *          1545 Ard Eevin Avenue
  32.  *          Glendale, CA 91202
  33.  */
  34.  
  35. #include <string.h>
  36. #include <XError.h>
  37. #include <XApplication.h>
  38. #include <XDataUtil.h>
  39. #include <XStdText.h>
  40. #include <XStdControls.h>
  41.  
  42. /************************************************************************/
  43. /*                                                                        */
  44. /*    Construction/Destruction                                            */
  45. /*                                                                        */
  46. /************************************************************************/
  47.  
  48. /*    XGStdEditText::XGStdEditText
  49.  *
  50.  *        Create this edit text box with the specified location
  51.  */
  52.  
  53. XGStdEditText::XGStdEditText(XGView *p, XGArgStream &s) : 
  54.     XGStdText(p,s,true)
  55. {
  56.     char buffer[256];
  57.     s.GetString(sizeof(buffer),buffer);
  58.     Init(buffer);
  59. }
  60.  
  61. XGStdEditText::XGStdEditText(XGView *p, XGSTextInitRecord &s) : 
  62.     XGStdText(p,s.v,true)
  63. {
  64.     Init(s.text);
  65. }
  66.  
  67. /*    XGStdEditText::~XGStdEditText
  68.  *
  69.  *        No longer edit the text in here
  70.  */
  71.  
  72. XGStdEditText::~XGStdEditText()
  73. {
  74. #if OPT_MACOS == 1
  75.     if (fEdit) TEDispose(fEdit);
  76. #endif
  77. }
  78.  
  79. /************************************************************************/
  80. /*                                                                        */
  81. /*    Standard Text Events                                                */
  82. /*                                                                        */
  83. /************************************************************************/
  84.  
  85. /*    XGStdEditText:DoDrawView
  86.  *
  87.  *        Draw this thing
  88.  */
  89.  
  90. void XGStdEditText::DoDrawView(Rect r)
  91. {
  92. #if OPT_MACOS == 1
  93.     Rect s;
  94.     
  95.     XGDraw draw(this,false);
  96.     
  97.     s = GetContentRect();
  98.     ViewToGlobal(&s);
  99.     DrawTextFrame(s);
  100.     
  101.     TEUpdate(&r,fEdit);
  102. #endif
  103.  
  104. #if OPT_WINOS == 1
  105.     #pragma unused(r)
  106. #endif
  107. }
  108.  
  109. /*    XGStdEditText::DoActivate
  110.  *
  111.  *        Handle activation/deactivation (if this thing has the focus)
  112.  */
  113.  
  114. void XGStdEditText::DoActivate(bool active)
  115. {
  116. #if OPT_MACOS == 1
  117.     XGDraw draw(this,false);
  118.     if (HasFocus() && IsEnabled()) {
  119.         if (active) TEActivate(fEdit);
  120.         else TEDeactivate(fEdit);
  121.     }
  122. #endif
  123.  
  124. #if OPT_WINOS == 1
  125.     #pragma unused(active)
  126. #endif
  127. }
  128.  
  129. /*    XGStdEditText::DoMouseDown
  130.  *
  131.  *        Handle clicks in this thing
  132.  */
  133.  
  134. bool XGStdEditText::DoMouseDown(Point where, short flags)
  135. {
  136. #if OPT_MACOS == 1
  137.     if (!IsEnabled()) return false;
  138.     if (!HasFocus()) SetFocus();        /* I get the focus! */
  139.  
  140.     /*
  141.      *    Now handle the click
  142.      */
  143.     
  144.     XGDraw draw(this,false);
  145.     ViewToGlobal(&where);
  146.     
  147.     TEClick(where,(flags & KKeyShift) ? true : false,fEdit);
  148.     return false;
  149. #endif
  150.  
  151. #if OPT_WINOS == 1
  152.     #pragma unused(where,flags)
  153. #endif
  154. }
  155.  
  156. /*    XGStdEditText::DoSizeView
  157.  *
  158.  *        Resize this view
  159.  */
  160.  
  161. void XGStdEditText::DoSizeView()
  162. {
  163. #if OPT_MACOS == 1
  164.     Rect r;
  165.     
  166.     XGDraw draw(this,false);
  167.     
  168.     r = GetContentRect();
  169.     ViewToGlobal(&r);
  170.     InsetRect(&r,3,3);
  171.     (**fEdit).viewRect = r;
  172.     if (r.right < 3000) r.right = 3000;
  173.     (**fEdit).destRect = r;
  174.     TECalText(fEdit);
  175.     InvalView();
  176. #endif
  177. }
  178.  
  179. /*    XGStdEditText::DoMoveView
  180.  *
  181.  *        Moves this view. Like DoSizeView
  182.  */
  183.  
  184. void XGStdEditText::DoMoveView()
  185. {
  186. #if OPT_MACOS == 1
  187.     DoSizeView();
  188. #endif
  189. }
  190.  
  191.  
  192. /*    XGStdEditText:GainFocus
  193.  *
  194.  *        Gain the focus. This enables this thing
  195.  */
  196.  
  197. void XGStdEditText::GainFocus()
  198. {
  199. #if OPT_MACOS == 1
  200.     if (IsActive() && IsEnabled()) {
  201.         XGDraw draw(this,false);
  202.         TEActivate(fEdit);
  203.     }
  204. #endif
  205. }
  206.  
  207. /*    XGStdEditText::LoseFocus
  208.  *
  209.  *        Lose the focus. This disables this thing
  210.  */
  211.  
  212. void XGStdEditText::LoseFocus()
  213. {
  214. #if OPT_MACOS == 1
  215.     if (IsActive() && IsEnabled()) {
  216.         XGDraw draw(this,false);
  217.         TEDeactivate(fEdit);
  218.     }
  219. #endif
  220. }
  221.  
  222.  
  223. /************************************************************************/
  224. /*                                                                        */
  225. /*    Dispatched events                                                    */
  226. /*                                                                        */
  227. /************************************************************************/
  228.  
  229. /*    XGStdEditText::ReceiveDispatch
  230.  *
  231.  *        This handles the following
  232.  *
  233.  *            KEventIdle
  234.  *            KEventDoMenuCommand
  235.  *                Cut/Copy/Paste/Clear
  236.  *            KEventGetMenuStatus
  237.  *                Cut/Copy/Paste/Clear
  238.  *            KEventKey
  239.  */
  240.  
  241. long XGStdEditText::ReceiveDispatch(long msg, long arg, void *parg)
  242. {
  243. #if OPT_MACOS == 1
  244.  
  245.     XGSMenuStatusRecord *e;
  246.     short cc;
  247.     
  248.     switch (msg) {
  249.         case KEventIdle:
  250.             if (IsActive() && IsEnabled() && HasFocus()) {
  251.                 XGDraw draw(this,false);
  252.                 TEIdle(fEdit);
  253.                 return 1;
  254.             }
  255.             break;
  256.  
  257.         case KEventGetMenuStatus:
  258.             switch (arg) {
  259.                 case KMenuEditCut:
  260.                 case KMenuEditCopy:
  261.                 case KMenuEditPaste:
  262.                 case KMenuEditClear:
  263.                     e = (XGSMenuStatusRecord *)parg;
  264.                     e->enable = true;
  265.                     return 1;
  266.             }
  267.             break;
  268.     
  269.         case KEventDoMenuCommand:
  270.             switch (arg) {
  271.                 case KMenuEditCut:
  272.                     {
  273.                         XGDraw draw(this,false);
  274.                         TECut(fEdit);
  275.                         return 1;
  276.                     }
  277.                 case KMenuEditCopy:
  278.                     {
  279.                         XGDraw draw(this,false);
  280.                         TECopy(fEdit);
  281.                         return 1;
  282.                     }
  283.                 case KMenuEditPaste:
  284.                     {
  285.                         XGDraw draw(this,false);
  286.                         TEPaste(fEdit);
  287.                         return 1;
  288.                     }
  289.                 case KMenuEditClear:
  290.                     {
  291.                         XGDraw draw(this,false);
  292.                         TEDelete(fEdit);
  293.                         return 1;
  294.                     }
  295.             }
  296.             break;
  297.         
  298.         case KEventKey:
  299.             cc = GETHIWORD(arg);
  300.             if (cc & KSpecialKey) {
  301.                 /* Trap and reconvert arrow keys back to Mac arrow keys */
  302.                 cc = GETLOWORD(arg);
  303.                 if (cc == KKeyLeftCursor) cc = 0x1C;
  304.                 else if (cc == KKeyRightCursor) cc = 0x1D;
  305.                 else if (cc == KKeyUpCursor) cc = 0x1E;
  306.                 else if (cc == KKeyDownCursor) cc = 0x1F;
  307.                 else break;
  308.             } else {
  309.                 cc = GETLOWORD(arg);
  310.                 if (IsSpecialKey(cc)) break;
  311.             }
  312.             
  313.             /*
  314.              *    My accepted key
  315.              */
  316.             
  317.             {
  318.                 XGDraw draw(this,false);
  319.                 TEKey(cc,fEdit);
  320.                 return 1;
  321.             }
  322.             
  323.         default:
  324.             break;
  325.     }
  326. #endif
  327.     
  328.     return XGView::ReceiveDispatch(msg,arg,parg);
  329. }
  330.  
  331. /************************************************************************/
  332. /*                                                                        */
  333. /*    Miscellaneous Support                                                */
  334. /*                                                                        */
  335. /************************************************************************/
  336.  
  337. /*    XGView::SetSelect
  338.  *
  339.  *        Set the current selection
  340.  */
  341.  
  342. void XGStdEditText::SetSelect(short s, short e)
  343. {
  344. #if OPT_MACOS == 1
  345.     XGDraw draw(this,false);
  346.     TESetSelect(s,e,fEdit);
  347. #endif
  348.  
  349. #if OPT_WINOS == 1
  350.     SendMessage(_GetHWND(),EM_SETSEL,(WPARAM)s,(LPARAM)e);
  351. #endif
  352. }
  353.  
  354. /*    XGView::GetSelect
  355.  *
  356.  *        Get the current selection range
  357.  */
  358.  
  359. void XGStdEditText::GetSelect(short *s, short *e)
  360. {
  361. #if OPT_MACOS == 1
  362.     *s = (**fEdit).selStart;
  363.     *e = (**fEdit).selEnd;
  364. #endif
  365.  
  366. #if OPT_WINOS == 1
  367.     long ss,ee;
  368.     
  369.     SendMessage(_GetHWND(),EM_GETSEL,(WPARAM)(LPDWORD)&ss,(LPARAM)(LPDWORD)&ee);
  370.     *s = ss;
  371.     *e = ee;
  372. #endif
  373. }
  374.  
  375. /************************************************************************/
  376. /*                                                                        */
  377. /*    Text manipulation                                                    */
  378. /*                                                                        */
  379. /************************************************************************/
  380.  
  381. /*    XGStdEditText::SetText
  382.  *
  383.  *        Set the text in this thing
  384.  */
  385.  
  386. void XGStdEditText::SetText(char *text)
  387. {
  388. #if OPT_MACOS == 1
  389.     XGDraw draw(this,false);
  390.     
  391.     TESetText(text,strlen(text),fEdit);
  392.     TESetSelect(0,strlen(text),fEdit);
  393. #endif
  394.  
  395. #if OPT_WINOS == 1
  396.     ::SetWindowText(_GetHWND(),text);
  397. #endif
  398. }
  399.  
  400. /*    XGStdEditText::GetText
  401.  *
  402.  *        Get the text from this thing
  403.  */
  404.  
  405. void XGStdEditText::GetText(char *text)
  406. {
  407. #if OPT_MACOS == 1
  408.     short len = 255;
  409.     if (len > (**fEdit).teLength) len = (**fEdit).teLength;
  410.     BlockMove(*((**fEdit).hText),text,len);
  411.     text[len] = 0;
  412. #endif
  413.  
  414. #if OPT_WINOS == 1
  415.     ::GetWindowText(_GetHWND(),text,256);
  416. #endif
  417. }
  418.  
  419. /************************************************************************/
  420. /*                                                                        */
  421. /*    Miscellaneous Hooks                                                    */
  422. /*                                                                        */
  423. /************************************************************************/
  424.  
  425. /*    XGView::IsSpecialKey
  426.  *
  427.  *        This determines the 'special keys'--those keys which are passed
  428.  *    up to the containing view. By default, these are the enter key and
  429.  *    the tab key
  430.  */
  431.  
  432. bool XGStdEditText::IsSpecialKey(char c)
  433. {
  434.     if (c == 0x0D) return true;
  435.     if (c == 0x09) return true;
  436.     return false;
  437. }
  438.  
  439. /*    XGView::DrawTextFrame
  440.  *
  441.  *        This draws the frame around the edited text
  442.  */
  443.  
  444. void XGStdEditText::DrawTextFrame(Rect r)
  445. {
  446. #if OPT_MACOS == 1
  447.     FrameRect(&r);
  448.     InsetRect(&r,1,1);
  449.     EraseRect(&r);
  450. #endif
  451.  
  452. #if OPT_WINOS == 1
  453.     #pragma unused(r)
  454. #endif
  455. }
  456.  
  457.  
  458. /************************************************************************/
  459. /*                                                                        */
  460. /*    Initialization                                                        */
  461. /*                                                                        */
  462. /************************************************************************/
  463.  
  464. /*    XGStdEditText::Init
  465.  *
  466.  *        Initialize this thing
  467.  */
  468.  
  469. void XGStdEditText::Init(char *buffer)
  470. {
  471.     long l;
  472.     
  473.     l = GetParent()->ReceiveDispatch(KEventGetFont,GetViewID(),(void *)&this);
  474. #if OPT_MACOS == 1
  475.     Rect r;
  476.     Rect t;
  477.     XGDraw draw(this,false);
  478.     
  479.     ::TextFont(GETHIWORD(l));
  480.     ::TextSize(GETLOWORD(l));
  481.     
  482.     /*
  483.      *    Create a textedit handle
  484.      */
  485.     
  486.     r = GetContentRect();
  487.     ViewToGlobal(&r);
  488.     InsetRect(&r,3,3);                        /* Inset away from border */
  489.     t = r;
  490.     if (t.right < 3000) t.right = 3000;        /* Arbitrary big number   */
  491.     
  492.     fEdit = TENew(&t,&r);                    /* Dest rect wider...      */
  493.     
  494.     /*
  495.      *    Initialize this thing
  496.      */
  497.     
  498.     TESetText(buffer,strlen(buffer),fEdit);
  499.     TEAutoView(true,fEdit);
  500.     TESetSelect(0,strlen(buffer),fEdit);
  501. #endif
  502.  
  503. #if OPT_WINOS == 1
  504.     Rect r;
  505.     HWND w;
  506.     
  507.     /*
  508.      *    Get the location of the control in the parent's coordinate
  509.      *    system
  510.      */
  511.      
  512.     r = GetContentRect();
  513.     if (GetParent()) {
  514.         ViewToGlobal(&r);
  515.         GetParent()->GlobalToView(&r);
  516.     }
  517.     
  518.     /*
  519.      *    Create the control window
  520.      */
  521.     
  522.     w = CreateWindow("EDIT",                    // control class
  523.                      buffer,                    // control title
  524.                      WS_CHILD | ES_AUTOHSCROLL | // control window flags
  525.                          WS_BORDER,
  526.                      r.left,
  527.                      r.top,
  528.                      r.right-r.left,
  529.                      r.bottom-r.top,            // control location
  530.                      GetParent()->_GetHWND(),    // container window
  531.                      NULL,                        // no menu
  532.                      _GInstance,                // My app instance
  533.                      NULL);                        // no additional args
  534.     if (w == NULL) {
  535.         throw XPostError("Unable to make editbox");
  536.     }
  537.     
  538.     _SetHWND(w);                                // set me as the window
  539.     SetProp(w,_GViewClass,(HANDLE)this);
  540.     if (IsVisible()) ShowWindow(w,SW_SHOW);
  541.     EnableWindow(w,IsEnabled());                // set me up
  542. #endif
  543. }
  544.